<template>
  <div class="product-con">
    <!-- 筛选 -->
    <el-card class="box-card data-card">
      <div slot="header" class="clearfix">
        <span><i class="iconfont .icon-sousuo-copy"></i>筛选搜索</span>
        <el-button
          style="float: right"
          type="primary"
          size="medium"
          @click="onSumbit"
          >查询</el-button
        >
        <el-button style="float: right" size="medium" @click="onReset"
          >重置</el-button
        >
      </div>
      <div>
        <el-form
          ref="form"
          :model="listQuery"
          label-width="80px"
          :inline="true"
        >
          <el-form-item label="输入商品:">
            <el-input
              v-model="listQuery.name"
              placeholder="商品名称"
              class="input-w"
            ></el-input>
          </el-form-item>
          <el-form-item label="商品货号:">
            <el-input
              v-model="listQuery.product_sn"
              placeholder="商品货号"
              class="input-w"
            ></el-input>
          </el-form-item>
          <el-form-item label="商品分类:">
            <el-cascader
              v-model="listQuery.product_category_id"
              :options="categoryOptions"
              @change="handleChange"
            ></el-cascader>
          </el-form-item>
          <el-form-item label="商品品牌:">
            <el-select
              v-model="listQuery.brand_id"
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="item in brankListOption"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="上架状态:">
            <el-select
              v-model="listQuery.publish_status"
              placeholder="全部"
              class="input-w"
            >
              <el-option label="下架" value="0"></el-option>
              <el-option label="上架" value="1"></el-option>
            </el-select>
          </el-form-item>
        </el-form>

        <el-form
          ref="form"
          :model="listQuery"
          label-width="80px"
          :inline="true"
          class="form-center"
        >
          <el-form-item label="审核状态">
            <el-select
              v-model="listQuery.verify_status"
              placeholder="全部"
              class="input-w"
            >
              <el-option label="未审核" value="0"></el-option>
              <el-option label="已审核" value="1"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
    </el-card>

    <!-- 数据卡片 -->
    <el-card class="box-card data-card">
      <div slot="header" class="clearfix">
        <span><i class="iconfont icon-dingdan1"></i>商品列表</span>
        <el-button
          style="float: right; padding: 3px"
          type="primary"
          size="medium"
          @click="onAdd"
          >添加</el-button
        >
      </div>
    </el-card>
    <!-- 商品列表 -->

    <el-table :data="productList" border style="width: 100%" class="listtable">
      <el-table-column prop="id" label="编号"> </el-table-column>

      <el-table-column label="商品图片">
        <template slot-scope="scope">
          <img :src="scope.row.pic" />
        </template>
      </el-table-column>
      <el-table-column prop="name" label="商品名称"> </el-table-column>

      <el-table-column label="商品图片">
        <template slot-scope="scope">
          <div>上架：{{ scope.row.publish_status | publish }}</div>
          <div>新品：{{ scope.row.new_status | publish }}</div>
          <div>推荐：{{ scope.row.recommand_status | publish }}</div>
        </template>
      </el-table-column>
      <el-table-column label="排序">
        <template slot-scope="scope">
          <div>上架：{{ scope.row.sort }}</div>
        </template>
      </el-table-column>

      <el-table-column prop="sale" label="销量"> </el-table-column>
      <el-table-column label="审核状态">
        <template slot-scope="scope">
          <div>上架：{{ scope.row.verify_status | verify }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180px">
        <template slot-scope="scope">
          <el-button
            @click="updateProduct(scope.row.id)"
            type="primary"
            plain
            size="small"
            >编辑</el-button
          >
          <el-button
            @click="delProduct(scope.row.id)"
            type="danger"
            size="small"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <!-- size-change 修改每条显示的条数 -->
    <!-- current-change 修改当前页页码 -->
    <!-- current-page 当前的页码 默认 给他一个1 -->
    <!-- page-sizes 当前的每页的数据，能够切换 -->
    <!-- page-size 当前每页有多少条 -->
    <!-- total 数据的总条数 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="listQuery.pageNum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="listQuery.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      class="page"
    >
    </el-pagination>
  </div>
</template>

<script>
// import axios from "axios";
/* fetchProductList 函数在 product.js中输出时没有使用 default；其他文件中引入时需要使用 {}
如果输出时使用了default,引入时不需要使用{} */

import {
  fetchProductList,
  fetchCategory,
  fetchBrand,
  delProduct,
} from "@/api/product.js";
export default {
  name: "product",
  data() {
    return {
      brankListOption: [], //品牌列表数据
      productList: [],
      // 这是传给后台的参数

      listQuery: {
        pageSize: 10, //默认每页显示10条
        pageNum: 1, //默认当前页面是第一页
        name: null,
        product_sn: null,
        publish_status: null,
        verify_status: null,
        brand_id: null,
        product_category_id: null, //分类的二级id
      },
      total: 0, //总条数
      categoryOptions: [], //分类的下拉列表的数据
    };
  },
  //在钩子函数中获取数据
  created() {
    // axios
    //   .get("http://www.yinruifang.cn//index/Api/product_list")
    //   .then((res) => {
    //     console.log(res.data.product);
    //     this.productList = res.data.product;
    //   });
    // fetchProductList().then((res) => {
    //   this.productList = res.product;
    // });
    this.getProductList();
    this.getBrand();
    this.getCategory();
  },
  methods: {
    //获取列表数据
    getProductList() {
      fetchProductList(this.listQuery).then((res) => {
        console.log(res);
        this.productList = res.product;
        this.total = res.total;
      });
    },
    // 获取品牌数据
    getBrand() {
      fetchBrand().then((res) => {
        console.log(res);
        this.brankListOption = res.data;
      });
    },
    handleClick: function (row) {
      console.log(row);
    },
    handleDelect: function (row) {
      console.log(row);
    },
    // 修改每一页的数据条数
    handleSizeChange: function (val) {
      console.log("修改页面条数的函数运行了", val);
      this.listQuery.pageSize = val;
      this.listQuery.pageNum = 1;
      this.getProductList();
    },
    handleCurrentChange: function (val) {
      console.log("修改当前的页码的函数运行了", val);
      this.listQuery.pageNum = val;
      this.getProductList();
    },
    onSumbit: function () {
      console.log("查询函数运行了");
      console.log(this.listQuery);
      // 重新获取数据
      this.getProductList();
    },
    onReset: function () {
      console.log("重置函数运行了");

      this.listQuery = {
        pageSize: 10, //默认每页显示10条
        pageNum: 1, //默认当前页面是第一页
        name: null,
        product_sn: null,
      };
      // 重新获取数据
      this.getProductList();
    },
    getCategory() {
      fetchCategory().then((res) => {
        console.log(265, res);
        var data = res.data;
        // 重组数据

        data.forEach((item) => {
          var children = [];
          // 添加子类
          item.children.forEach((i) => {
            children.push({
              value: i.id,
              label: i.name,
            });
          });
          this.categoryOptions.push({
            value: item.id,
            label: item.name,
            children: children,
          });
        });
        console.log(17, this.categoryOptions);
      });
    },
    handleChange: function (value) {
      console.log(value);
      if (value != null && value.length === 2) {
        this.listQuery.product_category_id = value[1];
      }
    },
    onAdd: function () {
      this.$router.push("/pms/productadd"); //返回添加页面
    },
    // 跳转到 修改页面
    updateProduct: function (id) {
      console.log(id);
      this.$router.push("/pms/productupdate/" + id);
    },
    // 删除的函数
    delProduct: function (id) {
      console.log(id);
      this.$confirm("此操作将永久删除该条商品数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delProduct({ id: id }).then((res) => {
            console.log(res);
            if (res.type == "success") {
              this.$message({
                type: "success",
                message: "删除成功!",
                duration: 2000,
              });
              // 2.删除后需要刷新数据
              this.getProductList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
            duration: 2000,
          });
        });
    },
  },
  filters: {
    verify: function (value) {
      return value ? "已审核" : "未审核";
    },
    publish: function (value) {
      return value ? "是" : "否";
    },
  },
};
</script>

<style  lang="scss">
.product-con {
  .data-card {
    margin-top: 10px;
    margin-bottom: 10px;
    .form-center {
      text-align: center;
    }
    .input-w {
      width: 140px;
    }
  }
  .listtable {
    text-align: center;
    margin-top: 10px;
    margin-bottom: 10px;
    img {
      width: 50px;
    }
  }
  .page {
    float: right;
  }
}
</style>